package com.chunfeng.mypen.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chunfeng.mypen.dao.TopicRecodingDao;
import com.chunfeng.mypen.entity.TopicRecoding;
import com.chunfeng.mypen.result.JsonRequest;
import com.chunfeng.mypen.result.exenum.RequestException;
import com.chunfeng.mypen.service.TopicRecodingService;
import com.chunfeng.mypen.vo.TopicRecodingVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Description: 做题历史记录实现类
 * @author: 小雷
 * @createTime: 2023-11-14 00:18:16
 */
@Service
public class TopicRecodingServiceImpl extends ServiceImpl<TopicRecodingDao, TopicRecoding> implements TopicRecodingService {

    @Autowired
    private TopicRecodingDao topicRecodingDao;

    /**
     * 条件查询所有试卷
     *
     * @param topicRecoding 题目历史实体类，包含所有查询条件
     * @return
     */
    @Override
    public JsonRequest<List<TopicRecodingVo>> lookTopicRecoding(TopicRecoding topicRecoding) {
        List<TopicRecodingVo> voList = topicRecodingDao.lookTopicRecoding(topicRecoding);
        if (voList.isEmpty()) {
            return JsonRequest.error(RequestException.SELECT_ERROR);
        }
        return JsonRequest.success(voList);
    }

    /**
     * 添加题目历史信息
     *
     * @param topicRecoding 需要添加的实体类
     * @return
     */
    @Override
    public JsonRequest<Integer> addTopicRecoding(TopicRecoding topicRecoding) {
        int insert = topicRecodingDao.insert(topicRecoding);
        if (insert < 1) {
            return JsonRequest.error(RequestException.INSERT_ERROR);
        }
        return JsonRequest.success(insert);
    }

    /**
     * 修改题目历史信息
     *
     * @param topicRecoding 需要修改的实体类
     * @return
     */
    @Override
    public JsonRequest<Integer> updateTopicRecoding(TopicRecoding topicRecoding) {
        int update = this.baseMapper.updateById(topicRecoding);
        if (update < 1) {
            return JsonRequest.error(RequestException.UPDATE_ERROR);
        }
        return JsonRequest.success(update);
    }

    /**
     * 删除题目历史信息
     *
     * @param ids 需要删除的题目历史id
     * @return
     */
    @Override
    public JsonRequest<Integer> deleteTopicRecoding(List<String> ids) {
        //判断删除的ids是否有值
        if (ids.isEmpty()) {
            return JsonRequest.error(RequestException.NOT_FOUND);
        }
        //判断删除的ids是否存在
        Integer count = this.baseMapper.selectCount(new LambdaQueryWrapper<TopicRecoding>()
                .in(TopicRecoding::getId, ids));
        if (count != ids.size()) {
            return JsonRequest.error(RequestException.NOT_FOUND);
        }
        //删除ids
        int delete = this.baseMapper.deleteBatchIds(ids);
        if (delete != count) {
            return JsonRequest.error(RequestException.DELETE_ERROR);
        }
        return JsonRequest.success(delete);
    }
}
